GDIR=../..

## Get files, includes and libraries needed for GIMI
include $(GDIR)/GIMI/Makefile.gimi

CC=gcc
CXX=g++
LD=g++
CFLAGS=-Wall $(DEBUG) $(DEFINITIONS) $(CINCLUDES) $(CMISCFLAGS)
CPPFLAGS=-Wall -ansi $(DEBUG) $(CPPINCLUDES) $(DEFINITIONS) $(CPPMISCFLAGS)
LDFLAGS=
DEBUG=-O0 -g3
CMISCFLAGS=
CPPMISCFLAGS=
DEFINITIONS=-DLINUX_OS -DGIMI_NO_DATATYPE_INCLUDES -DGIMI_CLIENT_API=10402

CINCLUDES=-I. $(INCGIMI)
CPPINCLUDES=-I. $(INCGIMI)

LIBS=$(LIBSGIMI)

SRCSC=
SRCSCPP=
CPP_GIMPING=gim_ping.cpp
CPP_GIMSD=gim_servicediscovery.cpp
CPP_GIMLC=gim_listclients.cpp

## Construct final lists of CPP and C objects
# Common objects
OBJSC=$(SRCSC:.c=.o) $(CGIMI:.c=.o)
OBJSCPP=$(SRCSCPP:.cpp=.opp) $(CPPGIMI:.cpp=.opp)

# Objects for different executables
OBJ_GIMPING=$(CPP_GIMPING:.cpp=.opp)
OBJ_GIMSD=$(CPP_GIMSD:.cpp=.opp)
OBJ_GIMLC=$(CPP_GIMLC:.cpp=.opp)

## Define compiles and links
COMPILEC=$(CC) $(CFLAGS) $(CINCLUDES) -c
COMPILECPP=$(CXX) $(CPPFLAGS) $(CPPINCLUDES) -c
LINK=$(LD) $(LDFLAGS) $(LIBS)

.PHONY: all clean

all: gim_ping gim_servicediscovery gim_listclients

gim_ping: $(OBJSC) $(OBJSCPP) $(OBJ_GIMPING)
		$(LINK) -o $@ $(OBJSC) $(OBJSCPP) $(OBJ_GIMPING)
		
gim_servicediscovery: $(OBJSC) $(OBJSCPP) $(OBJ_GIMSD)
		$(LINK) -o $@ $(OBJSC) $(OBJSCPP) $(OBJ_GIMSD)

gim_listclients: $(OBJSC) $(OBJSCPP) $(OBJ_GIMLC)
		$(LINK) -o $@ $(OBJSC) $(OBJSCPP) $(OBJ_GIMLC)

clean:
	rm -f $(OBJ_GIMPING) $(OBJ_GIMSD) $(OBJ_GIMLC) $(OBJSC) $(OBJSCPP) gim_ping \
	gim_servicediscovery gim_listclients *.out *~ *.d *.dpp

## Below this is dependency generation stuff, no need to change.
## Compile rule for all .o (.c) file
%.o:    %.c
				$(COMPILEC) -o $@ $<

## Compile rule for all .opp (.cpp) files
%.opp:	%.cpp
				$(COMPILECPP) -o $@ $<

## Dependency generation rule: For all required '.d' files
%.d:	  %.c
				$(CC) -MM $(CFLAGS) $< > $@.$$$$; \
				sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
				rm -f $@.$$$$

## Dependency generation rule: For all required '.dpp' files
%.dpp:	%.cpp
				$(CXX) -MM $(CPPFLAGS) $< > $@.$$$$; \
				sed 's,\($*\)\.o[ :]*,\1.opp $@ : ,g' < $@.$$$$ > $@; \
				rm -f $@.$$$$

%.h:	%.h

# Include dependency rules
-include $(SRCSCPP:.cpp=.dpp)
-include $(SRCSC:.c=.d)
